package com.jelly.web.modules.basics.sensitiveWord.service.impl;

import com.jelly.common.util.ListUtil;
import com.jelly.common.util.StringUtils;
import com.jelly.web.modules.basics.sensitiveWord.dao.SensitiveWordMapper;
import com.jelly.web.modules.basics.sensitiveWord.domain.SensitiveWord;
import com.jelly.web.modules.basics.sensitiveWord.domain.SensitiveWordExample;
import com.jelly.web.modules.basics.sensitiveWord.service.ISensitiveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * @创建人 ql
 * @创建时间 2018-10-09 17:13
 * @项目名称 jelly
 * @功能描述: 敏感词接口实现类
 **/
@Service
public class SensitiveWordServiceImpl implements ISensitiveWordService {

    @Autowired
    private SensitiveWordMapper sensitiveWordMapper;

    /**
     * 获取所有的敏感词
     * @return
     */
    @Override
    public Set<String> getSensitiveWordAll() {

        List<SensitiveWord> list = sensitiveWordMapper.selectByExample(new SensitiveWordExample());

        if(ListUtil.isNotList(list)){

            Set<String> set = new HashSet<>( ListUtil.getFieldValues(list,"badword"));

            return set;

        }

        return null;
    }
    /**
     * 条件查询敏感词
     * @param sensitiveWord
     * @return
     */
    @Override
    public List<SensitiveWord> getSensitiveWordAll(SensitiveWord sensitiveWord) {

        SensitiveWordExample example = new SensitiveWordExample();

        if(StringUtils.isNotBlank(sensitiveWord.getBadword())){

            example.createCriteria().andBadwordEqualTo(sensitiveWord.getBadword());

        }

        return sensitiveWordMapper.selectByExample(example);

    }

    /**
     * 主键查询敏感词
     * @param id
     * @return
     */
    @Override
    public SensitiveWord getSensitiveWordId(String id) {
        return sensitiveWordMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增敏感词
     * @param sensitiveWord
     * @return
     */
    @Override
    public boolean addSensitiveWord(SensitiveWord sensitiveWord) {

        sensitiveWord.setId(UUID.randomUUID().toString().replaceAll("-", ""));

        return sensitiveWordMapper.insertSelective(sensitiveWord) > 0;

    }

    /**
     * 主键修改敏感词
     * @param sensitiveWord
     * @return
     */
    @Override
    public boolean updatSensitiveWord(SensitiveWord sensitiveWord) {

        if(StringUtils.isBlank(sensitiveWord.getId())){

            sensitiveWord.setId(sensitiveWord.getBaseId());

        }

        return sensitiveWordMapper.updateByPrimaryKeySelective(sensitiveWord) > 0;

    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public boolean delectSensitiveWord(String id) {

        return sensitiveWordMapper.deleteByPrimaryKey(id) > 0;

    }

}
